/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import DAO.*;
import DTO.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author XuanHung
 */
public  class frmNhapPhongBanMoi extends javax.swing.JFrame {
     NhanVienDAO nvDAo;
     Datacontact dct;
     clsNhanVien nv;
     clsPhongBan  pb;
    /**
     * Creates new form frmNhapPhongBanMoi
     */
    private  void LoadData() throws Exception{
        List <clsNhanVien> arr = new ArrayList <>();
        nvDAo = new NhanVienDAO();
        arr = nvDAo.getData();
        for(clsNhanVien nv : arr){
            jcbxTruongPhong.addItem(nv.getM_iMaNhanVien());
        }
        
    }
    
    public frmNhapPhongBanMoi() throws Exception {
        initComponents();
        pb = new clsPhongBan();
        nv = new clsNhanVien();
        dct = new Datacontact();
        boolean Connect = dct.Connect();
        LoadData();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    
    /**
     *
     * @throws Exception
     */
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jtxtmaPhongBan = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jtxtTenPhongBan = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jcbxTruongPhong = new javax.swing.JComboBox();
        jbtnThem = new javax.swing.JButton();
        jbtnThoat = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("THÊM PHÒNG BAN");

        jLabel2.setText("Mã Phòng Ban");

        jtxtmaPhongBan.setText("MaPhongBan");

        jLabel3.setText("Tên Phòng Ban");

        jtxtTenPhongBan.setText("TenPhongBan");

        jLabel4.setText("Trưởng Phòng");

        jcbxTruongPhong.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select new leader" }));

        jbtnThem.setText("Thêm");
        jbtnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnThemActionPerformed(evt);
            }
        });

        jbtnThoat.setText("Thoát");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(148, 148, 148)
                        .addComponent(jbtnThem)
                        .addGap(18, 18, 18)
                        .addComponent(jbtnThoat))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jtxtmaPhongBan)
                            .addComponent(jtxtTenPhongBan)
                            .addComponent(jcbxTruongPhong, 0, 177, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(15, 15, 15)
                                .addComponent(jLabel1)))))
                .addContainerGap(21, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtxtmaPhongBan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtxtTenPhongBan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jcbxTruongPhong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnThem)
                    .addComponent(jbtnThoat))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnThemActionPerformed
        // TODO add your handling code here:
        try{
            int iMaPhongBan = Integer.parseInt(jtxtmaPhongBan.getText());
            String strTenPhongBan = jtxtTenPhongBan.getText();
            String [] str;
            str = jcbxTruongPhong.getSelectedItem().toString().split("-");
            int iMaTruongPhong  = Integer.parseInt(str[0]);
            PhongBanDAO pbd = new PhongBanDAO();
            pb = new clsPhongBan(iMaPhongBan, strTenPhongBan, iMaTruongPhong);
            pbd.luuPhongBan(pb, false);
            dct.close();
        }
        catch(NumberFormatException | SQLException ex){
            try {
                throw new Exception(ex.toString());
            } catch (Exception ex1) {
                Logger.getLogger(frmNhapPhongBanMoi.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
    }//GEN-LAST:event_jbtnThemActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmNhapPhongBanMoi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmNhapPhongBanMoi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmNhapPhongBanMoi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmNhapPhongBanMoi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    new frmNhapPhongBanMoi().setVisible(true);
                } catch (Exception ex) {
                    Logger.getLogger(frmNhapPhongBanMoi.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JButton jbtnThem;
    private javax.swing.JButton jbtnThoat;
    private javax.swing.JComboBox jcbxTruongPhong;
    private javax.swing.JTextField jtxtTenPhongBan;
    private javax.swing.JTextField jtxtmaPhongBan;
    // End of variables declaration//GEN-END:variables
}
